#include <string>

#include "Player.h"
#include "XMLParser.h"
#include "XMLWriter.h"


using namespace std;


/**
 * Load a saved character from a file
 * @param std::string strFileName - name of the file
 * @param std::vector<Class*> classes - list of classes to refer to when loading Player
 * @return void
 */
void Player::Load(std::string strFileName, std::vector<Class*> classes)
{
	string	strClassName = "";
	XMLParser appConfig(PLAYER_SAVEFILE_ROOTELEM, PLAYER_SAVEFILE_FIRSTELEM);

	appConfig.addATTROption(PLAYER_SAVEFILE_ATTR1);
	appConfig.addATTROption(PLAYER_SAVEFILE_ATTR2);
	appConfig.addATTROption(PLAYER_SAVEFILE_ATTR3);
	appConfig.addATTROption(PLAYER_SAVEFILE_ATTR4);
	appConfig.addATTROption(PLAYER_SAVEFILE_ATTR5);
	appConfig.addATTROption(PLAYER_SAVEFILE_ATTR6);
	appConfig.addATTROption(PLAYER_SAVEFILE_ATTR7);
	appConfig.addATTROption(PLAYER_SAVEFILE_ATTR8);
	appConfig.addATTROption(PLAYER_SAVEFILE_ATTR9);
	appConfig.addATTROption(PLAYER_SAVEFILE_ATTR10);
	appConfig.addATTROption(PLAYER_SAVEFILE_ATTR11);
	appConfig.addATTROption(PLAYER_SAVEFILE_ATTR12);
	appConfig.addATTROption(PLAYER_SAVEFILE_ATTR13);
	appConfig.addATTROption(PLAYER_SAVEFILE_ATTR14);
	appConfig.addATTROption(PLAYER_SAVEFILE_ATTR15);
	appConfig.addATTROption(PLAYER_SAVEFILE_ATTR16);
	appConfig.addATTROption(PLAYER_SAVEFILE_ATTR17);
	appConfig.addATTROption(PLAYER_SAVEFILE_ATTR18);
	appConfig.addATTROption(PLAYER_SAVEFILE_ATTR19);
	appConfig.addATTROption(PLAYER_SAVEFILE_ATTR20);
	appConfig.addATTROption(PLAYER_SAVEFILE_ATTR21);
	appConfig.addATTROption(PLAYER_SAVEFILE_ATTR22);
	appConfig.readPlayerConfigFile(strFileName.c_str(), this);
	appConfig.readClassFromPlayerConfigFile(strFileName.c_str(), strClassName);
	setClass(classes, strClassName);
}


/**
 * Save a character from a file
 * @param std::string strFileName - name of the file
 * @return void
 */
void Player::Save(std::string strFileName)
{
	XMLWriter	newWriter;
	char		itoaBuffer[PLAYER_ITOABUFFER_SIZE];

	newWriter.init(PLAYER_SAVEFILE_ROOTELEM, PLAYER_SAVEFILE_FIRSTELEM);

	newWriter.addAttribute(PLAYER_SAVEFILE_ATTR1, m_strName.c_str());
	_itoa_s(m_iHP, itoaBuffer, PLAYER_ITOABUFFER_SIZE, 10);
	newWriter.addAttribute(PLAYER_SAVEFILE_ATTR2, itoaBuffer);
	_itoa_s(m_iMaxHP, itoaBuffer, PLAYER_ITOABUFFER_SIZE, 10);
	newWriter.addAttribute(PLAYER_SAVEFILE_ATTR3, itoaBuffer);
	_itoa_s(m_iMP, itoaBuffer, PLAYER_ITOABUFFER_SIZE, 10);
	newWriter.addAttribute(PLAYER_SAVEFILE_ATTR4, itoaBuffer);
	_itoa_s(m_iMaxMP, itoaBuffer, PLAYER_ITOABUFFER_SIZE, 10);
	newWriter.addAttribute(PLAYER_SAVEFILE_ATTR5, itoaBuffer);
	_itoa_s(m_iXP, itoaBuffer, PLAYER_ITOABUFFER_SIZE, 10);
	newWriter.addAttribute(PLAYER_SAVEFILE_ATTR6, itoaBuffer);
	_itoa_s(m_iMaxXP, itoaBuffer, PLAYER_ITOABUFFER_SIZE, 10);
	newWriter.addAttribute(PLAYER_SAVEFILE_ATTR7, itoaBuffer);
	_itoa_s(m_iLevel, itoaBuffer, PLAYER_ITOABUFFER_SIZE, 10);
	newWriter.addAttribute(PLAYER_SAVEFILE_ATTR8, itoaBuffer);
	_itoa_s(m_iGold, itoaBuffer, PLAYER_ITOABUFFER_SIZE, 10);
	newWriter.addAttribute(PLAYER_SAVEFILE_ATTR9, itoaBuffer);
	_itoa_s(m_iPhysicalAttack, itoaBuffer, PLAYER_ITOABUFFER_SIZE, 10);
	newWriter.addAttribute(PLAYER_SAVEFILE_ATTR10, itoaBuffer);
	_itoa_s(m_iPhysicalDefense, itoaBuffer, PLAYER_ITOABUFFER_SIZE, 10);
	newWriter.addAttribute(PLAYER_SAVEFILE_ATTR11, itoaBuffer);
	_itoa_s(m_iMagicalAttack, itoaBuffer, PLAYER_ITOABUFFER_SIZE, 10);
	newWriter.addAttribute(PLAYER_SAVEFILE_ATTR12, itoaBuffer);
	_itoa_s(m_iMagicalDefense, itoaBuffer, PLAYER_ITOABUFFER_SIZE, 10);
	newWriter.addAttribute(PLAYER_SAVEFILE_ATTR13, itoaBuffer);
	_itoa_s(m_iEvasion, itoaBuffer, PLAYER_ITOABUFFER_SIZE, 10);
	newWriter.addAttribute(PLAYER_SAVEFILE_ATTR14, itoaBuffer);
	_itoa_s(m_iStealth, itoaBuffer, PLAYER_ITOABUFFER_SIZE, 10);
	newWriter.addAttribute(PLAYER_SAVEFILE_ATTR15, itoaBuffer);
	_itoa_s(m_iPerception, itoaBuffer, PLAYER_ITOABUFFER_SIZE, 10);
	newWriter.addAttribute(PLAYER_SAVEFILE_ATTR16, itoaBuffer);
	_itoa_s(m_iStrength, itoaBuffer, PLAYER_ITOABUFFER_SIZE, 10);
	newWriter.addAttribute(PLAYER_SAVEFILE_ATTR17, itoaBuffer);
	_itoa_s(m_iReflexes, itoaBuffer, PLAYER_ITOABUFFER_SIZE, 10);
	newWriter.addAttribute(PLAYER_SAVEFILE_ATTR18, itoaBuffer);
	_itoa_s(m_iIntelligence, itoaBuffer, PLAYER_ITOABUFFER_SIZE, 10);
	newWriter.addAttribute(PLAYER_SAVEFILE_ATTR19, itoaBuffer);
	_itoa_s(m_iPsychic, itoaBuffer, PLAYER_ITOABUFFER_SIZE, 10);
	newWriter.addAttribute(PLAYER_SAVEFILE_ATTR20, itoaBuffer);
	_itoa_s(m_iCharisma, itoaBuffer, PLAYER_ITOABUFFER_SIZE, 10);
	newWriter.addAttribute(PLAYER_SAVEFILE_ATTR21, itoaBuffer);
	newWriter.addAttribute(PLAYER_SAVEFILE_ATTR22, (m_class->getName()).c_str());
	newWriter.writeToFile(strFileName.c_str());
}
